﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VideoMonitorSystem.Model;
using System.Data;
using System.Data.SqlClient;

namespace VideoMonitorSystem.DAL
{
    /// <summary>
    /// 展示计划信息
    /// </summary>
    public class ShowPlanInfoService
    {
        private static ShowPlanInfo GetShowPlanInfoByDataRow(DataRow row)
        {
            ShowPlanInfo info = new ShowPlanInfo();
            info.SpID = Convert.ToInt32(row["spID"].ToString());
            info.ShowPlan = (ShowPlan)ShowPlanService.GetShowPlanByPID(Convert.ToInt32(row["pID"].ToString()));
            info.DeptInfo = (DeptInfo)DeptInfoService.GetDeptInfoByDID(Convert.ToInt32(row["dID"].ToString()));
            info.SwfFile = (SwfFile)SwfFileService.GetSwfFileByFID(Convert.ToInt32(row["fID"].ToString()));
            return info;
        }

        private static List<ShowPlanInfo> GetShowPlanInfoByDataTable(DataTable dataTable)
        {
            List<ShowPlanInfo> list = new List<ShowPlanInfo>();
            foreach(DataRow row in dataTable.Rows)
            {
                list.Add(GetShowPlanInfoByDataRow(row));
            }
            return list;
        }

        private static List<ShowPlanInfo> GetShowPlanInfoBySQL(string sql)
        {
            List<ShowPlanInfo> list = new List<ShowPlanInfo>();
            using(DataTable dataTable = DBHelper.GetDataTable(sql))
            {
                list = GetShowPlanInfoByDataTable(dataTable);
            }
            return list;
        }

        public static int AddShowPlanInfo(ShowPlanInfo showPlanInfo)
        {
            string sql = "INSERT INTO showPlanInfo(pID,dID,fID) values(@pID,@dID,@fID)";
            SqlParameter[] par = new SqlParameter[]
            {
                new SqlParameter("@pID",showPlanInfo.ShowPlan.PID),
                new SqlParameter("@dID",showPlanInfo.DeptInfo.DID),
                new SqlParameter("@fID",showPlanInfo.SwfFile.FID)
            };
            return DBHelper.GetScalar(sql,par);
        }

        public static ShowPlanInfo GetShowPlanInfoBySpID(int spID)
        {
            string sql = "SELECT * FROM showPlanInfo WHERE spID = @spID";
            DataRow row = DBHelper.GetDataTable(sql, new SqlParameter("@spID", spID)).Rows[0];
            return GetShowPlanInfoByDataRow(row);
        }

    }
}
